package com.cdplife.ci.core.common.ftp;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URL;
import java.util.Date;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.cdplife.ci.config.properties.OSSProperties;

import cn.stylefeng.roses.core.util.SpringContextHolder;

public class FtpOss {

    private static OSSProperties oss = (OSSProperties)SpringContextHolder.getBean(OSSProperties.class);
    
	public static void makeDir(OSSClient ossClient,String keySuffixWithSlash) throws Exception {
		// 创建dir1/dir2/dir3/时，已默认为您创建好了dir1/、dir1/dir2/前两级目录。
		if(keySuffixWithSlash.isEmpty()){
			keySuffixWithSlash = "dir1/dir2/dir3/";
		}
		ossClient.putObject(oss.getAliOSSBucketName(), keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
	}
	
	public static OSSClient createOSSClient(){
		return new OSSClient(oss.getAliOSSEndpoint(), oss.getAliOSSAccessKeyId(), oss.getAliOSSAccessKeySecret());
	}
	
	public static void uploadFile(OSSClient ossClient,String objectName, String localFile) throws Exception {
		if (ossClient == null) {
			throw new Exception("ossClient connection error!");
		}
		try{
			
			if(objectName.substring(0, 1).equals("/")){
				objectName = objectName.substring(1);
			}
			ossClient.putObject(oss.getAliOSSBucketName(), objectName, new File(localFile));
			
		} catch (OSSException oe) {
			throw new Exception(oe.getErrorCode());
        } catch (ClientException ce) {
			throw new Exception(ce.getMessage());
        }
	}

	public static void downloadFile(String objectName,String fileName) throws Exception {
		OSSClient ossClient = createOSSClient();
		// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
		if(objectName.substring(0, 1).equals("/")){
			objectName = objectName.substring(1);
		}
		try{
			ossClient.getObject(new GetObjectRequest(oss.getAliOSSBucketName(), objectName), new File(fileName));
		}catch(Exception e){
			throw e;
		}finally{
			disconnect(ossClient);
		}
	}

	public static void disconnect(OSSClient ossClient) {
		// 关闭OSSClient。
		ossClient.shutdown();
	}
	
	//获取图片访问连接
	public static String getImgUrl(String objectName)  {
		OSSClient ossClient = createOSSClient();
		URL url = null;
		try{
			if(objectName.length()>0 && objectName.substring(0, 1).equals("/")){
				objectName = objectName.substring(1);
			}
			// 设置URL过期时间为2小时。
			Date expiration = new Date(new Date().getTime() + 7200 * 72 * 1000);
			// 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
			url = ossClient.generatePresignedUrl(oss.getAliOSSBucketName(), objectName, expiration);
		}catch(Exception e){
			throw e;
		}finally{
			disconnect(ossClient);
		}
		disconnect(ossClient);
		return url.toString();
	}
	
	public static boolean isExistFile(OSSClient ossClient,String objectName) throws Exception {
		// 判断文件是否存在。
		if(objectName.substring(0, 1).equals("/")){
			objectName = objectName.substring(1);
		}
		boolean found = ossClient.doesObjectExist(oss.getAliOSSBucketName(), objectName);
		return found;
	}
	
	//删除文件
	public static void deleteFile(OSSClient ossClient,String objectName) throws Exception {
		if(objectName.substring(0, 1).equals("/")){
			objectName = objectName.substring(1);
		}
		ossClient.deleteObject(oss.getAliOSSBucketName(), objectName);
	}
}
